﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System;

#pragma warning disable 0618

public class Movement : MonoBehaviour
{
    public Animator animator;
    public GameObject player;
    public Rigidbody rigidbody;
    public float updateInterval = 0.5F;
    private double lastInterval;
    private int frames = 0;
    private float fps;
    private bool isMoving = false;

    public void MoveForward()
    {
        isMoving = true;
    }

    private void Awake()
    {
        BaseClass.Messenger.AddListener("JumpAni", JumpAni);
        BaseClass.Messenger.AddListener("OnFloorAni", OnFloor);
        BaseClass.Messenger.AddListener("OnSlide", OnSlide);
        BaseClass.Messenger.AddListener("RunAni", RunAni);
        BaseClass.Messenger.AddListener("CollideAni", CollideAni);
    }

    public void RunAni()
    {
        animator.SetFloat("Speed", rigidbody.velocity.magnitude);
    }

    public void CollideAni()
    {
        animator.SetBool("isCollided", true);
        rigidbody.AddForce(transform.forward * -300f);
        Invoke("CollideAniRollBack", 2f);
    }

    public void CollideAniRollBack()
    {
        animator.SetBool("isCollided", false);
    }

    void Start()
    {
        lastInterval = Time.realtimeSinceStartup;
        frames = 0;
    }
    void OnGUI()
    {
        GUILayout.Label(" " + fps.ToString("f2"));
    }
    public void Update()
    {
        //帧率计算
        ++frames;
        float timeNow = Time.realtimeSinceStartup;
        if (timeNow > lastInterval + updateInterval)
        {
            fps = (float)(frames / (timeNow - lastInterval));
            frames = 0;
            lastInterval = timeNow;
        }

    }

    public void JumpAni()
    {
        switch (animator.GetInteger("MoveState"))
        {
            case 0:
                animator.SetInteger("MoveState", 1);
                break;
            case 1:
                animator.SetInteger("MoveState", 2);
                break;
            case 2:
                animator.SetInteger("MoveState", 3);
                break;
            default:
                break;
        }
    }

    public void OnFloor()
    {
        animator.SetInteger("MoveState", 0);
    }

    public void OnSlide()
    {
        animator.SetInteger("MoveState", 4);
        Invoke("OnFloor", 1f);
    }



}

